home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / quicktimeintro / wiredsprites / common files / endianutilities.c < prev    next >
Encoding:
Text File  |  2000-10-06  |  4.6 KB  |  173 lines

  1. //////////
  2. //
  3. //    File:        EndianUtilities.c
  4. //
  5. //    Contains:    Utilities for managing the endian differences between operating systems.
  6. //
  7. //    Written by:    Tim Monroe
  8. //                Based on existing endian functions by various QT engineers.
  9. //
  10. //    Copyright:    © 1998 by Apple Computer, Inc., all rights reserved.
  11. //
  12. //    Change History (most recent first):
  13. //
  14. //       <1>         03/27/98    rtm        first file
  15. //
  16. //////////
  17.  
  18. #ifndef __ENDIANUTILITIES__
  19. #include "EndianUtilities.h"
  20. #endif
  21.  
  22.  
  23. //////////
  24. //
  25. // EndianUtils_FlipImageDescription
  26. // Convert an image description to big- or native-endian format.
  27. //
  28. //////////
  29.  
  30. static void EndianUtils_FlipImageDescription (Boolean theNtoB, ImageDescriptionHandle theIDH)
  31. {
  32.     (**theIDH).idSize            = EndianU32_NtoB((**theIDH).idSize);
  33.     (**theIDH).cType            = EndianU32_NtoB((**theIDH).cType);
  34.     (**theIDH).resvd1            = EndianU32_NtoB((**theIDH).resvd1);
  35.     (**theIDH).resvd2            = EndianU16_NtoB((**theIDH).resvd2);
  36.     (**theIDH).dataRefIndex        = EndianU16_NtoB((**theIDH).dataRefIndex);
  37.     (**theIDH).version            = EndianU16_NtoB((**theIDH).version);
  38.     (**theIDH).revisionLevel    = EndianU16_NtoB((**theIDH).revisionLevel);
  39.     (**theIDH).vendor            = EndianU32_NtoB((**theIDH).vendor);
  40.     (**theIDH).temporalQuality    = EndianU32_NtoB((**theIDH).temporalQuality);
  41.     (**theIDH).spatialQuality     = EndianU32_NtoB((**theIDH).spatialQuality);
  42.     (**theIDH).width            = EndianU16_NtoB((**theIDH).width);
  43.     (**theIDH).height            = EndianU16_NtoB((**theIDH).height);
  44.     (**theIDH).hRes                = EndianU32_NtoB((**theIDH).hRes);
  45.     (**theIDH).vRes                = EndianU32_NtoB((**theIDH).vRes);
  46.     (**theIDH).dataSize            = EndianU32_NtoB((**theIDH).dataSize);
  47.     (**theIDH).frameCount        = EndianU16_NtoB((**theIDH).frameCount);
  48.     (**theIDH).depth            = EndianU16_NtoB((**theIDH).depth);
  49.     (**theIDH).clutID            = EndianU16_NtoB((**theIDH).clutID);
  50.  
  51.     if ((**theIDH).clutID == 0 ) {
  52.         ImDesc    **myHandle = (ImDesc **)theIDH;
  53.         short    i;
  54.         
  55.         // make sure there really is a color table following the image description
  56.         if (GetHandleSize((Handle)theIDH) == sizeof(ImageDescription)) {
  57.             (**theIDH).clutID = -1;
  58.         } else {
  59.             short    ctSize = (**myHandle).ct.ctSize;
  60.  
  61.             if (!theNtoB)                    // not already native endian
  62.                 ctSize = EndianS16_NtoB(ctSize);
  63.         
  64.             (**myHandle).ct.ctSeed         = EndianU16_NtoB((**myHandle).ct.ctSeed);
  65.             (**myHandle).ct.ctSize         = EndianU16_NtoB((**myHandle).ct.ctSize);
  66.             (**myHandle).ct.ctFlags     = EndianU16_NtoB((**myHandle).ct.ctFlags);
  67.  
  68.             for (i = 0; i < ctSize + 1; i++) {
  69.                 (**myHandle).ct.ctTable[i].value        = EndianU16_NtoB((**myHandle).ct.ctTable[i].value);
  70.                 (**myHandle).ct.ctTable[i].rgb.red        = EndianU16_NtoB((**myHandle).ct.ctTable[i].rgb.red);
  71.                 (**myHandle).ct.ctTable[i].rgb.green    = EndianU16_NtoB((**myHandle).ct.ctTable[i].rgb.green);
  72.                 (**myHandle).ct.ctTable[i].rgb.blue        = EndianU16_NtoB((**myHandle).ct.ctTable[i].rgb.blue);
  73.             }
  74.         }
  75.     }
  76. }
  77.  
  78.  
  79. //////////
  80. //
  81. // EndianUtils_ImageDescription_NtoB
  82. // Convert an image description to big-endian format.
  83. //
  84. //////////
  85.  
  86. void EndianUtils_ImageDescription_NtoB (ImageDescriptionHandle theIDH)
  87. {
  88.     EndianUtils_FlipImageDescription(kNtoB, theIDH);
  89. }
  90.  
  91.  
  92. //////////
  93. //
  94. // EndianUtils_ImageDescription_BtoN
  95. // Convert an image description from big- to native-endian format.
  96. //
  97. //////////
  98.  
  99. void EndianUtils_ImageDescription_BtoN (ImageDescriptionHandle theIDH)
  100. {
  101.     EndianUtils_FlipImageDescription(kBtoN, theIDH);
  102. }
  103.  
  104.  
  105. //////////
  106. //
  107. // EndianUtils_MatrixRecord_NtoB
  108. // Convert a matrix record to big-endian format.
  109. //
  110. //////////
  111.  
  112. void EndianUtils_MatrixRecord_NtoB (MatrixRecord *theMatrix)
  113. {
  114. #if TARGET_RT_LITTLE_ENDIAN
  115.     int i, j;
  116.     
  117.     for (i = 0; i < 3; i++)
  118.         for (j = 0; j < 3; j++)
  119.             theMatrix->matrix[j][i] = EndianS32_NtoB(theMatrix->matrix[j][i]);
  120. #endif
  121. }
  122.  
  123.  
  124. //////////
  125. //
  126. // EndianUtils_RgnHandle_NtoB
  127. // Convert a region handle to big-endian format.
  128. //
  129. // We know that a region consists of a size, followed by a Rect, followed possibly by a stream of SInt16s,
  130. // so we can just flip size of region/sizeof(SInt16) SInt16s from native- to big-endian format.
  131. //
  132. //////////
  133.  
  134. void EndianUtils_RgnHandle_NtoB (RgnHandle theRgn)
  135. {
  136. #if TARGET_RT_LITTLE_ENDIAN
  137.     short            mySize;
  138.     SInt16            *mySInt16Ptr;
  139.     
  140.     if (theRgn && *theRgn) {
  141.         HLock((Handle)theRgn);
  142.         
  143.         mySize = (**((short **)theRgn))/2;
  144.         
  145.         for (mySInt16Ptr = (SInt16*)*theRgn; mySize > 0; mySize--, mySInt16Ptr++) 
  146.             *mySInt16Ptr = EndianU16_NtoB(*mySInt16Ptr);
  147.  
  148.         HUnlock((Handle)theRgn);
  149.     }
  150. #endif
  151. }
  152.  
  153.  
  154. //////////
  155. //
  156. // EndianUtils_Float_NtoB
  157. // Convert a floating-point value to big-endian format.
  158. //
  159. //////////
  160.  
  161. void EndianUtils_Float_NtoB (float *theFloat)
  162. {
  163. #if TARGET_RT_LITTLE_ENDIAN
  164.     unsigned long    *myLongPtr;
  165.     
  166.     myLongPtr = (unsigned long *)theFloat;
  167.     *myLongPtr = EndianU32_NtoB(*myLongPtr);
  168. #endif
  169. }
  170.  
  171.  
  172.  
  173.